package edu.nf.relaxenjoy.web.controller.app;

import com.github.pagehelper.PageInfo;
import edu.nf.relaxenjoy.common.result.ResultVO;
import edu.nf.relaxenjoy.common.validator.ValidatorUtils;
import edu.nf.relaxenjoy.common.validator.group.AddGroup;
import edu.nf.relaxenjoy.common.validator.group.DefaultGroup;
import edu.nf.relaxenjoy.common.validator.group.UpdateGroup;
import edu.nf.relaxenjoy.service.EsService;
import edu.nf.relaxenjoy.service.PlatformProducer;
import edu.nf.relaxenjoy.service.PostService;
import edu.nf.relaxenjoy.service.impl.EsServiceImpl;
import edu.nf.relaxenjoy.web.controller.BaseController;
import edu.nf.relaxenjoy.domain.dto.PostDTO;
import edu.nf.relaxenjoy.domain.vo.PostVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author zhu
 * @date 2024-12-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/post")
public class PostController extends BaseController {

    private final PostService postService;

    private final PlatformProducer platformProducer;

    private final EsService esService;

    /**
     * 获取所有的图片信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("getPage/{page}/{limit}")
    public ResultVO getPage(@PathVariable long page, @PathVariable long limit) {
        PageInfo<PostVo> pageData = postService.getPage(page, limit);
        return success(pageData);
    }

    /**
     * 根据专辑获取所有的图片信息
     *
     * @param page
     * @param limit
     * @param albumId
     * @return
     */
    @RequestMapping("getAllPostByAlbum/{page}/{limit}")
    public ResultVO getAllPostByAlbum(@PathVariable long page, @PathVariable long limit, String albumId, Integer type) {
        PageInfo<PostVo> pageInfo = postService.getAllImgByAlbum(page, limit, albumId, type);
        return success(pageInfo);
    }

    /**
     * 获取图片信息
     *
     * @param id
     * @return
     */
    @RequestMapping("getOne")
    public ResultVO getOne(@RequestParam("id") String id) {
        PostVo postVo = postService.getImgDetail(id);
        return success(postVo);
    }

    /**
     * 发布图片
     *
     * @param postDTO
     * @return
     */
    @RequestMapping("publish")
    public ResultVO publish(@RequestBody PostDTO postDTO) {
        ValidatorUtils.validateEntity(postDTO, AddGroup.class, DefaultGroup.class);
        Long id = postService.publish(postDTO);
        return success(id);
    }


    /**
     * 删除图片
     *
     * @param ids
     * @param uid
     * @return
     */
    @RequestMapping("deletePosts/{uid}")
    public ResultVO deletePosts(@RequestBody List<String> ids, @PathVariable String uid) {
        postService.deleteImgs(ids, uid);
        return success();
    }

    /**
     * 更新图片
     *
     * @param postDTO
     * @return
     */
    @RequestMapping("updateImgDetail")
    public ResultVO updateImgDetail(@RequestBody PostDTO postDTO) {
        ValidatorUtils.validateEntity(postDTO, UpdateGroup.class, DefaultGroup.class);
        Long id = postService.updatePost(postDTO);
        return success(id);
    }

    /**
     * 热榜信息
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("getHot/{page}/{limit}")
    public ResultVO getHot(@PathVariable Integer page, @PathVariable Integer limit) {
        PageInfo<PostVo> pageInfo = postService.getHot(page, limit);
        return success(pageInfo);
    }
}
